package com.example.demo_text;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.*;

@WebServlet("/AdmitUpdateStudentServlet")
public class AdmitUpdateStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");

        // 获取表单参数
        String idStr = request.getParameter("id");
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String ageStr = request.getParameter("age");

        int id, age;

        try {
            id = Integer.parseInt(idStr);
            age = Integer.parseInt(ageStr);
        } catch (NumberFormatException e) {
            response.sendRedirect("admitmanageStudent.jsp");
            return;
        }

        // 调用 DAO 方法更新学生信息（包含用户名）
        boolean success = UserDAO.updateStudentProfile(id, username, email, age);

        if (success) {
            // 修改成功后跳转回该学生的信息页面
            response.sendRedirect("manageStudent.jsp?id=" + id);
        } else {
            // 失败则返回原页面
            request.setAttribute("error", "更新失败，请稍后再试");
            request.getRequestDispatcher("manageStudent.jsp").forward(request, response);
        }
    }
}
